package com.whr.controller;

import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.whr.bean.*;
import com.whr.service.ProbService;
import com.whr.service.RoleService;
import com.whr.utils.ExcelUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/prob")
public class ProbController {
    @Resource
    private ProbService probService;
    @Resource
    private RoleService roleService;

    @RequestMapping("/list")
    public String list(HttpSession session, Model model) {
        List<Problem> probs = new ArrayList<>();
        User user = (User) session.getAttribute("user");
        String role = user.getRole();
        String flag = roleService.findFlag(role);
        if ("系统管理".equals(flag)){
        probs = probService.findAll();
        }
        if ("职能部".equals(flag)){
            probs = probService.findZN(flag);
        }
        if ("院系部".equals(flag)){
            probs = probService.findYX(flag);
        }
        model.addAttribute("probs", probs);
        return "probList";
    }

    @RequestMapping("/add")
    public String add(Problem prob) {
        probService.saveProb(prob);
        return "redirect:/prob/list";
    }

    @RequestMapping("/toadd")
    public String toadd() {
        return "probAdd";
    }

    @RequestMapping("/toedit")
    public String toedit(Integer checks, Model model) {
        Problem prob = probService.findById(checks);
        model.addAttribute("prob", prob);
        return "probEdit";
    }

    @RequestMapping("/edit")
    public String edit(Problem prob) {
        probService.updateProb(prob);
        return "redirect:/prob/list";
    }

    @RequestMapping("/del")
    public String del(Integer[] checks) {
        probService.delProb(checks);
        return "redirect:/prob/check";
    }

    @RequestMapping("/detail/{pid}")
    public String detail(@PathVariable("pid") Integer pid, Model model) {
        Problem prob = probService.findById(pid);
        List<Reply> replies = probService.replyByPid(pid);
        List<Append> appends = probService.appendByPid(pid);
        Double raty = probService.ratyByPid(pid);
        model.addAttribute("prob", prob);
        model.addAttribute("raty", raty);
        model.addAttribute("replies", replies);
        model.addAttribute("appends", appends);
        return "detail";
    }

    @RequestMapping("/append/{pid}/{text}")
    public String detail(@PathVariable("pid") Integer pid, @PathVariable("text") String text) {
        probService.addAppend(pid, text);
        return "redirect:/prob/list";
    }

    @RequestMapping("/reply/{pid}/{text}")
    public String reply(@PathVariable("pid") Integer pid, @PathVariable("text") String text) {
        probService.addReply(pid, text);
        return "redirect:/prob/check";
    }

    @RequestMapping("/check")
    public String check(HttpSession session,Model model) {
        List<Problem> probs = new ArrayList<>();
        User user = (User) session.getAttribute("user");
        String role = user.getRole();
        String flag = roleService.findFlag(role);
        if ("系统管理".equals(flag)){
            probs = probService.findAllOrder();
        }
        if ("职能部".equals(flag)){
            probs = probService.findAllZN(flag);
        }
        if ("院系部".equals(flag)){
            probs = probService.findAllYX(flag);
        }

        List<String> rolename = roleService.selRoleName();
        model.addAttribute("rolename", rolename);
        model.addAttribute("probs", probs);
        return "checkList";
    }

    @RequestMapping("/urg")
    public String urg(Integer[] checks) {
        probService.urgProb(checks);
        return "redirect:/prob/check";
    }

    @RequestMapping("/fine")
    public String fine(Integer[] checks) {
        probService.fineProb(checks);
        return "redirect:/prob/check";
    }

    @RequestMapping("/end")
    public String end(Integer[] checks) {
        probService.endProb(checks);
        return "redirect:/prob/check";
    }

    @RequestMapping("/score/{raty}/{pid}")
    public String raty(@PathVariable("pid") Integer pid, @PathVariable("raty") Double raty) {
        probService.addRaty(pid, raty);
        return "redirect:/prob/list";
    }

    @RequestMapping("/editRole/{role}")
    public String editRole(@PathVariable("role") String role, Integer[] checks) {
        for (Integer pid : checks) {
            probService.updateRole(role, pid);
        }
        return "redirect:/prob/check";
    }

    @RequestMapping("/answer")
    public String answer(HttpSession session,Model model) {
        List<Problem> probs = new ArrayList<>();
        User user = (User) session.getAttribute("user");
        String role = user.getRole();
        String flag = roleService.findFlag(role);
        if ("系统管理".equals(flag)){
            probs = probService.findAllOrder();
        }
        if ("职能部".equals(flag)){
            probs = probService.findAllZN(flag);
        }
        if ("院系部".equals(flag)){
            probs = probService.findAllYX(flag);
        }
        model.addAttribute("probs", probs);
        return "answerList";
    }

    @RequestMapping("/enda")
    public String enda(Integer[] checks) {
        probService.endProb(checks);
        return "redirect:/prob/answer";
    }

    @RequestMapping("/ans/{pid}/{text}")
    public String ans(@PathVariable("pid") Integer pid, @PathVariable("text") String text) {
        probService.addReply(pid, text);
        return "redirect:/prob/answer";
    }

    @RequestMapping("/over")
    public String over(Model model) {
        List<Problem> probs = probService.findOver();
        model.addAttribute("probs", probs);
        return "overList";
    }

    @RequestMapping("/upover/{pid}/{pover}")
    public String upover(@PathVariable("pid") Integer pid, @PathVariable("pover") String pover) {
        probService.upOver(pid, pover);
        return "redirect:/prob/answer";
    }

    @RequestMapping("/pass")
    public String pass(Integer[] checks) {
        probService.pass(checks);
        return "redirect:/prob/over";
    }

    @RequestMapping("/unpass")
    public String unpass(Integer[] checks) {
        probService.unpass(checks);
        return "redirect:/prob/over";
    }

    @RequestMapping("/report")
    public String repo(Model model) {
        List<Problem> probs = probService.findRep();
        model.addAttribute("probs", probs);
        return "report";
    }

    @RequestMapping("/search")
    public String search(Report report, Model model) throws ParseException {
        List<Problem> probs = probService.searchRep(report);
        model.addAttribute("probs", probs);
        return "report";
    }

    @RequestMapping("/chart")
    public String chart(Model model) throws JsonProcessingException {
        List<Chart> charts = probService.chart();
        ObjectMapper mapper = new ObjectMapper();
        String values = mapper.writeValueAsString(charts);
        model.addAttribute("values", values);
        return "charts";
    }

    @RequestMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response) {

        JSONArray ja = probService.genJsonArry();
        Map<String, String> headMap = probService.genHeadMap();//获取属性-列头
        String title = "信息数据报表";
        ExcelUtils.downloadExcelFile(title, headMap, ja, response);
    }

}
